﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class AdminPagesPage : WatiNBase
    {
        #region Constructors

        public AdminPagesPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public AdminPagesPage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region Images

        public Image AddNewPageImage
        {
            get { return ContentPane.Image(Find.ByTitle("Add New Page")); }
        }
        public Image DeleteSelectedPageImage
        {
            get { return ContentPane.Image(Find.ByTitle("Delete Selected Page")); }
        }

        public Image EditSelectedPageImage
        {
            get { return ContentPane.Image(Find.ByTitle("Edit Selected Page")); }
        }
        public Image MovePageBottomButton
        {
            get { return IEInstance.Image(Find.ByTitle("Move Selected Page to the Bottom of Current Level")); }
        }
        public Image MovePageDownButton
        {
            get { return IEInstance.Image(Find.ByTitle("Move Selected Page Down In Current Level")); }
        }
        public Image MovePageLeftButton
        {
            get { return IEInstance.Image(Find.ByTitle("Move Selected Page Up One Hierarchical Level")); }
        }
        public Image MovePageRightButton
        {
            get { return IEInstance.Image(Find.ByTitle("Move Selected Page Down One Hierarchical Level")); }
        }
        public Image MovePageTopButton
        {
            get { return IEInstance.Image(Find.ByTitle("Move Selected Page to the Top of Current Level")); }
        }
        public Image MovePageUpButton
        {
            get { return IEInstance.Image(Find.ByTitle("Move Selected Page Up In Current Level")); }
        }

        #endregion

        #region SelectList
        public SelectList PagesSelectList
        {
            get { return ContentPane.SelectList(Find.ById(s => s.EndsWith("Tabs_lstTabs"))); }
        }
        #endregion

        #region Div
        public Div PagesPanel
        {
            get { return ContentPane.Div(Find.ById("dnnTabsModule")); }
        }
        #endregion

        #region Span Collections
        public SpanCollection PageSpans
        {
            get { return IEInstance.Spans.Filter(Find.ByClass("rtIn")); }
        }
        #endregion

        #region Link Collections
        public LinkCollection PageFunctionLinks
        {
            get { return IEInstance.Links.Filter(Find.ByClass("rmLink")); }
        }
        #endregion

        #region TextFields
        public TextField BulkAddPageField
        {
            get { return IEInstance.TextField(Find.ById(s => s.EndsWith("Tabs_txtBulk"))); }
        }
        #endregion

        #region Links
        public Link AddPageLink
        {
            get { return PageFunctionLinks.Filter(Find.ByText(s => s.Contains("Add Page(s)")))[0]; }
        }
        public Link HidePageLink
        {
            get { return PageFunctionLinks.Filter(Find.ByText(s => s.Contains("Hide from navigation")))[0]; }
        }
        public Link CreatePageLink
        {
            get { return IEInstance.Link(Find.ByTitle("Create Page(s)")); }
        }
        #endregion

        #endregion

        #region Public Methods
        public void AddChildPage(string childName, string parentName)
        {
            Span homeSpan = PageSpans.Filter(Find.ByText(s => s.Contains(parentName)))[0];
            homeSpan.FireEvent("oncontextmenu");
            System.Threading.Thread.Sleep(1000);
            AddPageLink.Click();
            BulkAddPageField.Value = childName;
            CreatePageLink.Click();
        }

        public void AddHiddenPage(string pageName)
        {
            AddPage(pageName);
            System.Threading.Thread.Sleep(2000);
            Span pageSpan = PageSpans.Filter(Find.ByText(s => s.Contains(pageName)))[0];
            pageSpan.FireEvent("oncontextmenu");
            System.Threading.Thread.Sleep(1000);
            HidePageLink.Click();
        }

        public void OldAddPage(string pageName)
        {
            AddNewPageImage.ClickNoWait();
            PageSettingsPage newPage = new PageSettingsPage(this);
            newPage.AddPage(pageName);
        }

        public void AddPage(string pageName)
        {
            Span homeSpan = PageSpans.Filter(Find.ByText(s => s.Contains("My Website")))[0];
            homeSpan.FireEvent("oncontextmenu");
            System.Threading.Thread.Sleep(1000);
            AddPageLink.Click();
            BulkAddPageField.Value = pageName;
            CreatePageLink.Click();
        }

        public void AddPageAfter(string pageName, string afterPage)
        {
            //Not Relevant anymore
            AddNewPageImage.ClickNoWait();
            PageSettingsPage newPage = new PageSettingsPage(this);
            newPage.AddPageAfter(pageName, afterPage);
        }

        public void AddPageGiveUserViewPermission(string pageName, string userName, string displayName)
        {
            AddPage(pageName);
            PageSettingsPage newPage = new PageSettingsPage(this);
            newPage.GiveUserViewPermissionsForPage(userName, displayName);
        }

        public int GetPageIndex(string pageName)
        {
            int index = 0;
            foreach (Span curSpan in PageSpans)
            {
                if (curSpan.InnerHtml.Contains(pageName))
                {
                    return index;
                }
                index++;
            }
            return -1;
        }
        #endregion
    }
}
